﻿Imports VN.INS.BL.BC
Imports System.Data
Imports System.IO
Imports Microsoft.Office.Interop

Partial Class Devida_ReporteAvanceSemanalActividades
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            'carga inicial
            ListaPPER()
            ListaPeriodo()
            ListaRegion(ddlPPER.SelectedValue.ToString)
            ListaAmbito(ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString)
            ListaEjecutor(ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString)
        End If
    End Sub

    Public Sub ListaPPER()
        'carga de combo pper
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPPER("")
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("DESCRIPCION") = "--Todos--"
        drusuariorow("PPER") = ""
        objData.Rows.Add(drusuariorow)
        ddlPPER.DataSource = objData
        ddlPPER.DataTextField = "DESCRIPCION"
        ddlPPER.DataValueField = "PPER"
        ddlPPER.DataBind()
        ddlPPER.SelectedValue = ""
    End Sub

    Private Sub ListaPeriodo()
        'carga de combo periodo
        Dim objGeneral As GeneralBC
        Dim objData As DataTable
        objGeneral = New GeneralBC
        objData = New DataTable
        objData = objGeneral.ListaPeriodoReporte("0", "DESC", Session("Año"))
        ddlPeriodo.DataSource = objData
        ddlPeriodo.DataTextField = "DESCRIPCION"
        ddlPeriodo.DataValueField = "PERIODO"
        ddlPeriodo.DataBind()
    End Sub

    Public Sub ListaRegion(ByVal strPPER As String)
        'carga de combo region
        Dim objGeneral As GeneralBC
        Dim objData As DataTable
        objGeneral = New GeneralBC
        objData = New DataTable
        objData = objGeneral.ListaRegion(strPPER)
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("NOMBRE") = "--Todos--"
        drusuariorow("UBIGEO") = ""
        objData.Rows.Add(drusuariorow)
        ddlRegion.DataSource = objData
        ddlRegion.DataTextField = "NOMBRE"
        ddlRegion.DataValueField = "UBIGEO"
        ddlRegion.DataBind()
        ddlRegion.SelectedValue = ""
    End Sub

    Public Sub ListaAmbito(ByVal strPPER As String, ByVal strRegion As String)
        'carga de combo ambito
        Dim objReporte As ReporteBC
        Dim objData As DataTable
        objReporte = New ReporteBC
        objData = New DataTable
        objData = objReporte.ListaAmbito(strPPER, strRegion)
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("DESCRIPCION") = "--Todos--"
        drusuariorow("CODIGO") = ""
        objData.Rows.Add(drusuariorow)
        ddlAmbito.DataSource = objData
        ddlAmbito.DataTextField = "DESCRIPCION"
        ddlAmbito.DataValueField = "CODIGO"
        ddlAmbito.DataBind()
        ddlAmbito.SelectedValue = ""
    End Sub

    Public Sub ListaEjecutor(ByVal strPPER As String, ByVal strRegion As String, ByVal strAmbito As String)
        'carga de combo ejecutor
        Dim objReporte As ReporteBC
        Dim objData As DataTable
        objReporte = New ReporteBC
        objData = New DataTable
        objData = objReporte.ListaEjecutor(strPPER, strRegion, strAmbito)
        Dim drusuariorow As DataRow = objData.NewRow()
        drusuariorow("DESCRIPCION") = "--Todos--"
        drusuariorow("EJECUTOR") = ""
        objData.Rows.Add(drusuariorow)
        ddlEjecutor.DataSource = objData
        ddlEjecutor.DataTextField = "DESCRIPCION"
        ddlEjecutor.DataValueField = "EJECUTOR"
        ddlEjecutor.DataBind()
        ddlEjecutor.SelectedValue = ""
    End Sub

    Protected Sub gvPPER_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvPPER.RowDataBound
        Dim objReporteBC As ReporteBC = New ReporteBC
        Dim dttip As DataTable
        If e.Row.RowType = DataControlRowType.DataRow Then  'Recorremos las filas de la grilla gvPPER
            'Capturamos los campos ocultos
            Dim hfPPER As HiddenField = CType(e.Row.FindControl("hfPPER"), HiddenField)
            Dim hfAbreviatura As HiddenField = CType(e.Row.FindControl("hfAbreviatura"), HiddenField)
            Dim hfNUM As HiddenField = CType(e.Row.FindControl("hfNUM"), HiddenField)
            Dim hfMONTO_TOTAL_INVERTIDO As HiddenField = CType(e.Row.FindControl("hfMONTO_TOTAL_INVERTIDO"), HiddenField)
            Dim hfMONTO_TRANSFERIDO As HiddenField = CType(e.Row.FindControl("hfMONTO_TRANSFERIDO"), HiddenField)
            Dim hfMONTO_PROGRAMADO As HiddenField = CType(e.Row.FindControl("hfMONTO_PROGRAMADO"), HiddenField)
            Dim hfMONTO_COMPROMETIDO As HiddenField = CType(e.Row.FindControl("hfMONTO_COMPROMETIDO"), HiddenField)
            Dim hfMONTO_DEVENGADO As HiddenField = CType(e.Row.FindControl("hfMONTO_DEVENGADO"), HiddenField)
            Dim hfMONTO_GIRADO As HiddenField = CType(e.Row.FindControl("hfMONTO_GIRADO"), HiddenField)
            ViewState("Abreviatura") = hfAbreviatura.Value.ToString
            ViewState("NUM") = hfNUM.Value.ToString
            ViewState("MONTO_PRESUPUESTADO") = hfMONTO_TOTAL_INVERTIDO.Value.ToString
            ViewState("MONTO_TRANSFERIDO") = hfMONTO_TRANSFERIDO.Value.ToString
            ViewState("MONTO_PROGRAMADO") = hfMONTO_PROGRAMADO.Value.ToString
            ViewState("MONTO_COMPROMETIDO") = hfMONTO_COMPROMETIDO.Value.ToString
            ViewState("MONTO_DEVENGADO") = hfMONTO_DEVENGADO.Value.ToString
            ViewState("MONTO_GIRADO") = hfMONTO_GIRADO.Value.ToString
            'Cargamos Los Tipos por PPER
            dttip = objReporteBC.ListaTipoPPER(ddlPeriodo.SelectedValue.ToString, ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString)
            Dim IDpper As String = hfPPER.Value.ToString
            ViewState("PPER") = IDpper
            'Filtramos la consulta segun el PPER y Periodo
            Dim vst As DataView = New DataView(dttip, "PPER='" + IDpper + "' AND PERIODO = '" + ddlPeriodo.SelectedValue.ToString + "'", "", DataViewRowState.CurrentRows)
            'Obtenemos la grilla anidada y la cargamos con los datos filtrados.
            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvTipo")
            gdv.DataSource = vst
            gdv.DataBind()
        ElseIf e.Row.RowType = DataControlRowType.Header Then 'Recorremos la cabecera
            'Cargamos los datos de cabecera vacio
            dttip = objReporteBC.ListaTotalSabana("x", "", "", "", "")
            'Obtenemos la grilla anidada en la cabeceray la llenamos con fila vacia.
            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvTipo_Cabecera")
            gdv.DataSource = dttip
            gdv.DataBind()
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
        End If
    End Sub

    Public Sub RDB_Tipo(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then 'Recorremos las filas de la grilla
            'Pintamos el fondo de blanco.
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            'Capturamos los campos ocultos.
            Dim hfTipo As HiddenField = CType(e.Row.FindControl("hfTipo1"), HiddenField)
            Dim hfPPER As HiddenField = CType(e.Row.FindControl("hfPPER1"), HiddenField)
            Dim hfAbreviatura1 As HiddenField = CType(e.Row.FindControl("hfAbreviatura1"), HiddenField)
            Dim hfMONTO_PRESUPUESTADO1 As HiddenField = CType(e.Row.FindControl("hfMONTO_PRESUPUESTADO1"), HiddenField)
            Dim hfMONTO_TRANSFERIDO1 As HiddenField = CType(e.Row.FindControl("hfMONTO_TRANSFERIDO1"), HiddenField)
            Dim hfMONTO_PROGRAMADO1 As HiddenField = CType(e.Row.FindControl("hfMONTO_PROGRAMADO1"), HiddenField)
            Dim hfMONTO_COMPROMETIDO1 As HiddenField = CType(e.Row.FindControl("hfMONTO_COMPROMETIDO1"), HiddenField)
            Dim hfMONTO_DEVENGADO1 As HiddenField = CType(e.Row.FindControl("hfMONTO_DEVENGADO1"), HiddenField)
            Dim hfMONTO_GIRADO1 As HiddenField = CType(e.Row.FindControl("hfMONTO_GIRADO1"), HiddenField)
            Dim hfGIRADO_MES1 As HiddenField = CType(e.Row.FindControl("hfGIRADO_MES1"), HiddenField)
            Dim hfGIRADO_ANUAL1 As HiddenField = CType(e.Row.FindControl("hfGIRADO_ANUAL1"), HiddenField)
            Dim hfNUM1 As HiddenField = CType(e.Row.FindControl("hfNUM1"), HiddenField)
            ViewState("TIPO") = hfTipo.Value.ToString
            ViewState("Abreviatura") = hfAbreviatura1.Value.ToString
            ViewState("NUM1") = hfNUM1.Value.ToString
            ViewState("MONTO_PRESUPUESTADO1") = hfMONTO_PRESUPUESTADO1.Value.ToString
            ViewState("MONTO_TRANSFERIDO1") = hfMONTO_TRANSFERIDO1.Value.ToString
            ViewState("MONTO_PROGRAMADO1") = hfMONTO_PROGRAMADO1.Value.ToString
            ViewState("MONTO_COMPROMETIDO1") = hfMONTO_COMPROMETIDO1.Value.ToString
            ViewState("MONTO_DEVENGADO1") = hfMONTO_DEVENGADO1.Value.ToString
            ViewState("MONTO_GIRADO1") = hfMONTO_GIRADO1.Value.ToString
            ViewState("GIRADO_MES1") = hfGIRADO_MES1.Value.ToString
            ViewState("GIRADO_ANUAL1") = hfGIRADO_ANUAL1.Value.ToString
            Dim objReporte As ReporteBC
            Dim objData1 As DataTable
            objReporte = New ReporteBC
            Dim vst As DataView
            If hfTipo.Value.ToString = "2" Then         'Si es tipo Actividad
                'Cargamos los productos para el PPER correspondiente
                objData1 = New DataTable
                objData1 = objReporte.ProductoXPPER(hfPPER.Value.ToString)
                vst = New DataView(objData1, "TIPO_PROYECTO_ACTIVIDAD='" + hfTipo.Value.ToString + "'", "", DataViewRowState.CurrentRows)
            ElseIf hfTipo.Value.ToString = "1" Then     'Si es Proyecto
                'Cargamos los proyectos por el PPER correspondiente
                objData1 = New DataTable
                objData1 = objReporte.ProyectoXPPER(hfPPER.Value.ToString)
                vst = New DataView(objData1, "TIPO_PROYECTO_ACTIVIDAD='" + hfTipo.Value.ToString + "'", "", DataViewRowState.CurrentRows)
            Else                                        'Si es Acción Común
                'Cargamos los proyectos por el PPER correspondiente
                objData1 = New DataTable
                objData1 = objReporte.POAXPPER(hfPPER.Value.ToString, hfTipo.Value.ToString)
                vst = New DataView(objData1, "TIPO_PROYECTO_ACTIVIDAD='" + hfTipo.Value.ToString + "' AND PPER = '" + hfPPER.Value.ToString + "'", "", DataViewRowState.CurrentRows)
            End If
            'Obtenemos la grilla de detalle anidada y la cargamos con filtro.
            Dim gdv As GridView = CType(e.Row.FindControl("gvDetalle"), GridView)           
            gdv.DataSource = vst
            gdv.DataBind()
        ElseIf e.Row.RowType = DataControlRowType.Header Then 'Recorremos la cabecera
            Dim objReporte As ReporteBC
            Dim objData1 As DataTable
            objReporte = New ReporteBC
            objData1 = New DataTable
            'Cargamos datos fde consulta de vaica
            objData1 = objReporte.ProductoXPPER("0")
            Dim vst As DataView
            vst = New DataView(objData1, "TIPO_PROYECTO_ACTIVIDAD='0' AND PPER = '0'", "", DataViewRowState.CurrentRows)
            'Obtenemos la grilla de DetalleCabecera anidada y la cargamos con filtro vacio.
            Dim gdv As GridView = CType(e.Row.FindControl("gvDetalle_Cabecera"), GridView)
            gdv.DataSource = vst
            gdv.DataBind()
        End If
    End Sub

    Public Sub RDB_Tipo_Cabecera(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            Dim objReporteBC As ReporteBC = New ReporteBC
            Dim objData1 As DataTable
            objData1 = objReporteBC.ProductoXPPER("0")
            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvTipo_Cabecera1")
            If Not (gdv Is Nothing) Then
                gdv.DataSource = objData1
                gdv.DataBind()
            End If            
        End If
    End Sub

    Public Sub RDB_Tipo_Cabecera1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            Dim objReporteBC As ReporteBC = New ReporteBC
            Dim dttip As DataTable
            dttip = objReporteBC.ListaTotalSabana(ddlPeriodo.SelectedValue.ToString, ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString)
            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvTipo_Cabecera2")
            gdv.DataSource = dttip
            gdv.DataBind()
        End If
    End Sub

    Public Sub RDB_Tipo_Cabecera2(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(83, 141, 213)
            For i As Int16 = 0 To 14
                e.Row.Cells(i).ForeColor = Drawing.Color.White
                e.Row.Cells(i).Font.Bold = True
                e.Row.Cells(i).Font.Name = "Tahoma"
                e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Right
            Next
            e.Row.Cells(1).ColumnSpan = 4
            'e.Row.Cells(15).Visible = False
            e.Row.Cells(16).Visible = False            
            e.Row.Cells(1).Text = "TOTAL PPER DEVIDA"
            e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
        End If
    End Sub

    Public Sub RDB_Detalle(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then 'Recorremos las filas de la grilla.
            'Coloreamos de blanco las filas
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            'Capturamos los campos ocultos.
            Dim hfTipo As HiddenField = CType(e.Row.FindControl("hfTipo2"), HiddenField)
            Dim hfPPER As HiddenField = CType(e.Row.FindControl("hfPPER2"), HiddenField)
            Dim hfPRODUCTO As HiddenField = CType(e.Row.FindControl("hfPRODUCTO2"), HiddenField)
            Dim hfDescripcion2 As HiddenField = CType(e.Row.FindControl("hfDescripcion2"), HiddenField)
            Dim hfNUM2 As HiddenField = CType(e.Row.FindControl("hfNUM2"), HiddenField)
            ViewState("TIPO") = hfTipo.Value.ToString
            ViewState("Descripcion") = hfDescripcion2.Value.ToString
            ViewState("NUM") = hfNUM2.Value.ToString
            ViewState("PRODUCTO") = hfPRODUCTO.Value.ToString
            'Obtenemos la grila gvPOA anidada
            Dim gdv As GridView = CType(e.Row.FindControl("gvPOA"), GridView)
            Dim vst As DataView
            Dim objReporte As ReporteBC
            Dim objData1 As DataTable
            objReporte = New ReporteBC
            objData1 = New DataTable
            'Cargamos los datos de detalle
            objData1 = objReporte.ReporteSabanaDetalle(ddlPeriodo.SelectedValue.ToString, hfPPER.Value.ToString, hfTipo.Value.ToString, hfPRODUCTO.Value.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString)
            vst = New DataView(objData1, "TIPO_PROYECTO_ACTIVIDAD='" & hfTipo.Value.ToString & _
                               "' AND PPER='" & hfPPER.Value & "'", _
                               "", DataViewRowState.CurrentRows)
            ViewState("NumID") = 0.0
            ViewState("PRESUPUESTO_MODIFICADO") = 0
            ViewState("MONTO_TRANSFERIDO") = 0
            ViewState("MONTO_PROGRAMADO") = 0
            ViewState("MONTO_COMPROMETIDO") = 0
            ViewState("MONTO_DEVENGADO") = 0
            ViewState("MONTO_GIRADO") = 0
            ViewState("GIRADO_MES") = 0
            ViewState("GIRADO_ANUAL") = 0
            If hfTipo.Value.ToString = "3" Then
                gdv.ShowHeader = False
            Else
                gdv.ShowHeader = True
                ViewState("NumID") = vst.Table.Rows.Count
                For i As Int16 = 0 To vst.Table.Rows.Count - 1
                    ViewState("PRESUPUESTO_MODIFICADO") = ViewState("PRESUPUESTO_MODIFICADO") + Convert.ToDouble(IIf(vst.Table.Rows(i).Item("PRESUPUESTO_MODIFICADO").ToString = "", 0, vst.Table.Rows(i).Item("PRESUPUESTO_MODIFICADO").ToString))
                    ViewState("MONTO_TRANSFERIDO") = ViewState("MONTO_TRANSFERIDO") + Convert.ToDouble(IIf(vst.Table.Rows(i).Item("MONTO_TRANSFERIDO").ToString = "", 0, vst.Table.Rows(i).Item("MONTO_TRANSFERIDO").ToString))
                    ViewState("MONTO_PROGRAMADO") = ViewState("MONTO_PROGRAMADO") + Convert.ToDouble(IIf(vst.Table.Rows(i).Item("MONTO_PROGRAMADO").ToString = "", 0, vst.Table.Rows(i).Item("MONTO_PROGRAMADO").ToString))
                    ViewState("MONTO_COMPROMETIDO") = ViewState("MONTO_COMPROMETIDO") + Convert.ToDouble(IIf(vst.Table.Rows(i).Item("MONTO_COMPROMETIDO").ToString = "", 0, vst.Table.Rows(i).Item("MONTO_COMPROMETIDO").ToString))
                    ViewState("MONTO_DEVENGADO") = ViewState("MONTO_DEVENGADO") + Convert.ToDouble(IIf(vst.Table.Rows(i).Item("MONTO_DEVENGADO").ToString = "", 0, vst.Table.Rows(i).Item("MONTO_DEVENGADO").ToString))
                    ViewState("MONTO_GIRADO") = ViewState("MONTO_GIRADO") + Convert.ToDouble(IIf(vst.Table.Rows(i).Item("MONTO_GIRADO").ToString = "", 0, vst.Table.Rows(i).Item("MONTO_GIRADO").ToString))
                Next
            End If
            gdv.DataSource = vst
            gdv.DataBind()
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(191, 191, 191)
            e.Row.Cells(0).ForeColor = Drawing.Color.Black
            Dim vst As DataView
            Dim objReporte As ReporteBC
            Dim objData1 As DataTable
            objReporte = New ReporteBC
            objData1 = New DataTable
            objData1 = objReporte.ReporteSabanaDetalle(ddlPeriodo.SelectedValue.ToString, "", "", "", "", "", "")
            vst = New DataView(objData1, "TIPO_PROYECTO_ACTIVIDAD='0'", _
                               "", DataViewRowState.CurrentRows)
            Dim gdv As GridView = CType(e.Row.FindControl("gvPOA_Cabecera"), GridView)
            gdv.DataSource = vst
            gdv.DataBind()
            e.Row.Font.Bold = False
            e.Row.ForeColor = Drawing.Color.Black
            e.Row.HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
        End If
    End Sub

    Public Sub RDB_Detalle_Cabecera1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If ViewState("Abreviatura").ToString = "PIRDAIS" Then
            e.Row.BackColor = Drawing.Color.FromArgb(146, 208, 80)
        ElseIf ViewState("Abreviatura").ToString = "PTCD" Then
            e.Row.BackColor = Drawing.Color.FromArgb(177, 160, 199)
        Else
            e.Row.BackColor = Drawing.Color.FromArgb(141, 180, 226)
        End If
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).Text = ViewState("NUM").ToString
            e.Row.Cells(1).Text = "TOTAL " + ViewState("Abreviatura").ToString
            For i As Int16 = 0 To 14
                e.Row.Cells(i).ForeColor = Drawing.Color.Black
                e.Row.Cells(i).Font.Bold = True
                e.Row.Cells(i).Font.Name = "Tahoma"
                e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Right
            Next
            e.Row.Cells(1).ColumnSpan = 4
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
            e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
            e.Row.Cells(2).Text = CDec(Math.Round(CDec(ViewState("MONTO_PRESUPUESTADO")), 2)).ToString("#,##0")
            e.Row.Cells(3).Text = CDec(Math.Round(CDec(ViewState("MONTO_TRANSFERIDO")), 2)).ToString("#,##0")
            e.Row.Cells(4).Text = CDec(Math.Round(CDec(ViewState("MONTO_PROGRAMADO")), 2)).ToString("#,##0")
            e.Row.Cells(5).Text = CDec(Math.Round(CDec(ViewState("MONTO_COMPROMETIDO")), 2)).ToString("#,##0")
            e.Row.Cells(6).Text = CDec(Math.Round(CDec(ViewState("MONTO_DEVENGADO")), 2)).ToString("#,##0")
            e.Row.Cells(7).Text = CDec(Math.Round(CDec(ViewState("MONTO_GIRADO")), 2)).ToString("#,##0")
            e.Row.Cells(8).Text = CDec(Math.Round(CDec(IIf(ViewState("MONTO_PROGRAMADO") = 0, 0, ViewState("MONTO_GIRADO") / ViewState("MONTO_PROGRAMADO"))), 2)).ToString("#,##0 %")
            e.Row.Cells(9).Text = CDec(Math.Round(CDec(IIf(ViewState("MONTO_PRESUPUESTADO") = 0, 0, ViewState("MONTO_GIRADO") / ViewState("MONTO_PRESUPUESTADO"))), 2)).ToString("#,##0 %")
            'e.Row.Cells(15).Visible = False
            e.Row.Cells(16).Visible = False
        End If
    End Sub

    Public Sub RDB_Detalle_Cabecera(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            Dim objData1 As DataTable
            Dim objReporte As ReporteBC
            objReporte = New ReporteBC
            Dim vst As DataView
            ViewState("TIPO") = "2"
            ViewState("PPER") = "PPE-0001"
            objData1 = New DataTable
            objData1 = objReporte.POAXPPER(ViewState("PPER").ToString, "2")
            vst = New DataView(objData1, "TIPO_PROYECTO_ACTIVIDAD='" + ViewState("TIPO").ToString + "' AND PPER = '" + ViewState("PPER").ToString + "'", "", DataViewRowState.CurrentRows)

            Dim gdv As GridView = e.Row.Cells(0).FindControl("gvDetalle_Cabecera1")
            gdv.DataSource = vst
            gdv.DataBind()
        End If
    End Sub

    Public Sub RDB_POA(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
            Select Case e.Row.Cells(16).Text
                Case "1"
                    e.Row.Cells(16).BackColor = Drawing.Color.FromArgb(0, 176, 80)
                Case "2"
                    e.Row.Cells(16).BackColor = Drawing.Color.FromArgb(255, 192, 0)
                Case "3"
                    e.Row.Cells(16).BackColor = Drawing.Color.FromArgb(255, 0, 0)
            End Select
            e.Row.Cells(16).Text = ""
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            If ViewState("TIPO") = "1" Then
                e.Row.Cells(0).Text = ViewState("NumID").ToString
                For i As Int16 = 0 To 14
                    e.Row.Cells(i).ForeColor = Drawing.Color.Black
                    e.Row.Cells(i).Font.Bold = False
                    e.Row.Cells(i).Font.Name = "Tahoma"
                    e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Right
                Next
                e.Row.HorizontalAlign = HorizontalAlign.Right
                e.Row.Cells(1).ColumnSpan = 4
                e.Row.Cells(13).Visible = False
                e.Row.Cells(14).Visible = False
                e.Row.Cells(15).Visible = False
                e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
                e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Left
                e.Row.Cells(2).Text = CDec(Math.Round(CDec(ViewState("PRESUPUESTO_MODIFICADO")), 2)).ToString("#,##0")
                e.Row.Cells(3).Text = CDec(Math.Round(CDec(ViewState("MONTO_TRANSFERIDO")), 2)).ToString("#,##0")
                e.Row.Cells(4).Text = CDec(Math.Round(CDec(ViewState("MONTO_PROGRAMADO")), 2)).ToString("#,##0")
                e.Row.Cells(5).Text = CDec(Math.Round(CDec(ViewState("MONTO_COMPROMETIDO")), 2)).ToString("#,##0")
                e.Row.Cells(6).Text = CDec(Math.Round(CDec(ViewState("MONTO_DEVENGADO")), 2)).ToString("#,##0")
                e.Row.Cells(7).Text = CDec(Math.Round(CDec(ViewState("MONTO_GIRADO")), 2)).ToString("#,##0")
                e.Row.Cells(8).Text = CDec(Math.Round(CDec(IIf(ViewState("MONTO_PROGRAMADO") = 0, 0, ViewState("MONTO_GIRADO") / ViewState("MONTO_PROGRAMADO"))), 2)).ToString("#,##0 %")
                e.Row.Cells(9).Text = CDec(Math.Round(CDec(IIf(ViewState("PRESUPUESTO_MODIFICADO") = 0, 0, ViewState("MONTO_GIRADO") / ViewState("PRESUPUESTO_MODIFICADO"))), 2)).ToString("#,##0 %")
                e.Row.Cells(10).Text = ""
                e.Row.Cells(11).Text = ""
                e.Row.Cells(12).Text = ""
                e.Row.Cells(16).Text = ""
                e.Row.Cells(17).Text = ""
                If ViewState("Abreviatura").ToString = "PIRDAIS" Then
                    For i As Int16 = 0 To 18
                        e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(146, 208, 80)
                    Next
                    e.Row.Cells(1).Text = ViewState("Descripcion").ToString.ToUpper
                ElseIf ViewState("Abreviatura").ToString = "PTCD" Then
                    For i As Int16 = 0 To 18
                        e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(177, 160, 199)
                    Next
                    e.Row.Cells(1).Text = ViewState("Descripcion").ToString.ToUpper
                Else
                    For i As Int16 = 0 To 18
                        e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(141, 180, 226)
                    Next
                    e.Row.Cells(1).Text = ViewState("Descripcion").ToString.ToUpper
                End If
            ElseIf ViewState("TIPO") = "2" Then
                e.Row.Cells(0).Text = ViewState("NumID").ToString
                For i As Int16 = 0 To 15
                    e.Row.Cells(i).ForeColor = Drawing.Color.Black
                    e.Row.Cells(i).Font.Bold = False
                    e.Row.Cells(i).Font.Name = "Tahoma"
                    e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Right
                Next
                e.Row.HorizontalAlign = HorizontalAlign.Right
                e.Row.Cells(1).ColumnSpan = 4
                e.Row.Cells(13).Visible = False
                e.Row.Cells(14).Visible = False
                e.Row.Cells(15).Visible = False
                e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
                e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Left
                e.Row.Cells(2).Text = CDec(Math.Round(CDec(ViewState("PRESUPUESTO_MODIFICADO")), 2)).ToString("#,##0")
                e.Row.Cells(3).Text = CDec(Math.Round(CDec(ViewState("MONTO_TRANSFERIDO")), 2)).ToString("#,##0")
                e.Row.Cells(4).Text = CDec(Math.Round(CDec(ViewState("MONTO_PROGRAMADO")), 2)).ToString("#,##0")
                e.Row.Cells(5).Text = CDec(Math.Round(CDec(ViewState("MONTO_COMPROMETIDO")), 2)).ToString("#,##0")
                e.Row.Cells(6).Text = CDec(Math.Round(CDec(ViewState("MONTO_DEVENGADO")), 2)).ToString("#,##0")
                e.Row.Cells(7).Text = CDec(Math.Round(CDec(ViewState("MONTO_GIRADO")), 2)).ToString("#,##0")
                e.Row.Cells(8).Text = CDec(Math.Round(CDec(IIf(ViewState("MONTO_PROGRAMADO") = 0, 0, ViewState("MONTO_GIRADO") / ViewState("MONTO_PROGRAMADO"))), 2)).ToString("#,##0 %")
                e.Row.Cells(9).Text = CDec(Math.Round(CDec(IIf(ViewState("PRESUPUESTO_MODIFICADO") = 0, 0, ViewState("MONTO_GIRADO") / ViewState("PRESUPUESTO_MODIFICADO"))), 2)).ToString("#,##0 %")
                e.Row.Cells(10).Text = ""
                e.Row.Cells(11).Text = ""
                e.Row.Cells(12).Text = ""
                e.Row.Cells(16).Text = ""
                e.Row.Cells(17).Text = ""
                If ViewState("Abreviatura").ToString = "PIRDAIS" Then
                    For i As Int16 = 0 To 18
                        e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(146, 208, 80)
                    Next
                    e.Row.Cells(1).Text = "PRODUCTO " + ViewState("NUM").ToString + " " + ViewState("Descripcion").ToString.ToUpper
                ElseIf ViewState("Abreviatura").ToString = "PTCD" Then
                    For i As Int16 = 0 To 18
                        e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(177, 160, 199)
                    Next
                    e.Row.Cells(1).Text = "PRODUCTO " + ViewState("NUM").ToString + " " + ViewState("Descripcion").ToString.ToUpper
                Else
                    For i As Int16 = 0 To 18
                        e.Row.Cells(i).BackColor = Drawing.Color.FromArgb(141, 180, 226)
                    Next
                    e.Row.Cells(1).Text = "PRODUCTO " + ViewState("NUM").ToString + " " + ViewState("Descripcion").ToString.ToUpper
                End If
            End If
        End If
    End Sub

    Public Sub RDB_POA_Cabecera(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
        ElseIf e.Row.RowType = DataControlRowType.Header Then
            e.Row.Cells(0).Text = ViewState("NUM1").ToString
            For i As Int16 = 0 To 14
                e.Row.Cells(i).ForeColor = Drawing.Color.Black
                e.Row.Cells(i).Font.Bold = False
                e.Row.Cells(i).Font.Name = "Tahoma"
                e.Row.Cells(i).HorizontalAlign = HorizontalAlign.Right
            Next
            e.Row.HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
            e.Row.Cells(1).ColumnSpan = 4
            'e.Row.Cells(15).Visible = False
            e.Row.Cells(16).Visible = False
            e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Center
            e.Row.Cells(2).Text = CDec(Math.Round(CDec(ViewState("MONTO_PRESUPUESTADO1")), 2)).ToString("#,##0")
            e.Row.Cells(3).Text = CDec(Math.Round(CDec(ViewState("MONTO_TRANSFERIDO1")), 2)).ToString("#,##0")
            e.Row.Cells(4).Text = CDec(Math.Round(CDec(ViewState("MONTO_PROGRAMADO1")), 2)).ToString("#,##0")
            e.Row.Cells(5).Text = CDec(Math.Round(CDec(ViewState("MONTO_COMPROMETIDO1")), 2)).ToString("#,##0")
            e.Row.Cells(6).Text = CDec(Math.Round(CDec(ViewState("MONTO_DEVENGADO1")), 2)).ToString("#,##0")
            e.Row.Cells(7).Text = CDec(Math.Round(CDec(ViewState("MONTO_GIRADO1")), 2)).ToString("#,##0")
            e.Row.Cells(8).Text = CDec(Math.Round(CDec(IIf(ViewState("MONTO_PROGRAMADO1") = 0, 0, ViewState("MONTO_GIRADO1") / ViewState("MONTO_PROGRAMADO1"))), 2)).ToString("#,##0 %")
            e.Row.Cells(9).Text = CDec(Math.Round(CDec(IIf(ViewState("MONTO_PRESUPUESTADO1") = 0, 0, ViewState("MONTO_GIRADO1") / ViewState("MONTO_PRESUPUESTADO1"))), 2)).ToString("#,##0 %")
            If ViewState("TIPO") = "1" Then
                e.Row.Cells(1).Text = "PROYECTOS"
            ElseIf ViewState("TIPO") = "2" Then
                e.Row.Cells(1).Text = "PRODUCTOS"
            Else
                e.Row.Cells(1).Text = "ACCIONES COMUNES"
            End If
        End If
    End Sub

    Protected Sub btnGenerar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGenerar.Click
        Dim objReporte As ReporteBC
        Dim objData As DataTable
        objReporte = New ReporteBC
        objData = New DataTable
        objData = objReporte.ListaPPER(ddlPeriodo.SelectedValue.ToString, ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString, ddlEjecutor.SelectedValue.ToString)
        'Llenamos la grilla principal PPER segun los filtros.
        gvPPER.DataSource = objData
        gvPPER.DataBind()
        Me.fsSabana.Style("display") = "inline"
    End Sub

    Private Sub ExportarGrilla(ByVal grilla As GridView)
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        '----------------------------
        Dim Tabla As New Table
        Dim Celda As New TableCell()
        Celda.RowSpan = 1
        Celda.ColumnSpan = 18
        'Commeted by Tigrito 15/05/2013
        'Celda.Text = "AVANCE SEMANAL DE ACTIVIDADES Y PROYECTOS DE LOS PPERs EJECUTOR DEVIDA AL " + ddlPeriodo.SelectedItem.Text
        Celda.Text = "AVANCE SEMANAL DE ACTIVIDADES Y PROYECTOS DE LOS PPERs AL " + ddlPeriodo.SelectedItem.Text
        Celda.Font.Name = "Calibri"
        Celda.Font.Size = 12
        Celda.Font.Bold = True
        Celda.ForeColor = Drawing.Color.Black
        Celda.BackColor = Drawing.Color.White
        Celda.HorizontalAlign = HorizontalAlign.Center
        Celda.VerticalAlign = VerticalAlign.Top
        Dim Fila As New TableRow()
        Fila.Cells.Add(Celda)
        Tabla.Rows.AddAt(0, Fila)
        Dim headerTable As String = "<table style='padding:0; margin:0;' cellpadding='0' cellspacing='0'>" + _
                                    "<tr><td colspan='18' style='font-family:Tahoma; text-align:center; font-weight:bold' >AVANCE SEMANAL DE ACTIVIDADES Y PROYECTOS DE LOS PPERs AL " + ddlPeriodo.SelectedItem.Text + " <BR/> AÑO " + Session("Año") + "</td>" + _
                                    "</tr>" + _
                                    "<tr style='background-color:#0070C0; text-align:center; color:White; font-family:Tahoma'; vertical-align:top>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>N°</td> " + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Entidades Ejecutoras</td> " + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Código</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Región</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Nombre de la Actividad o Proyecto </td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Presupuesto Modificado</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Monto transferido</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' colspan='4'>Ejecución financiera S/.</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' colspan='2'>Avance %</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Monitoreador</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Especialista</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>Observaciones</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>ESTADO</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333' rowspan='2'>ACCION</td>" + _
                                    "</tr>" + _
                                    "<tr style='background-color:#0070C0; text-align:center; color:White; font-family:Tahoma; vertical-align:top' > " + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333'>Programado al mes reportado</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333'>Comprometido</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333'>Devengado</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333'>Girado</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333'>Al mes</td>" + _
                                    "<td style='padding:0; margin:0; border: 1px solid #333333'>Anual</td>" + _
                                    "</tr>" + _
                                    "</table>"
        '----------------------------
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(Tabla)
        form.InnerHtml = headerTable
        form.Controls.Add(grilla)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=AVANCE_FINANCIERO_SEMANAL_" + Now.Date.Day.ToString + "_" + Now.Date.Month.ToString + "_" + Now.Date.Year.ToString + "_" + Now.Hour.ToString + "_" + Now.Minute.ToString + "_" + Now.Second.ToString + ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()
    End Sub

    Protected Sub btnExportar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportar.Click
        ExportarGrilla(gvPPER)
    End Sub

    Protected Sub ddlPPER_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPPER.SelectedIndexChanged
        'carga combos enlazados
        ListaRegion(ddlPPER.SelectedValue.ToString)
        ListaAmbito(ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString)
        ListaEjecutor(ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString)
        Me.fsSabana.Style("display") = "none"
    End Sub

    Protected Sub ddlRegion_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRegion.SelectedIndexChanged
        'carga combos enlazados
        ListaAmbito(ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString)
        ListaEjecutor(ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString)
        Me.fsSabana.Style("display") = "none"
    End Sub

    Protected Sub ddlAmbito_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlAmbito.SelectedIndexChanged
        'carga combo enlazado
        ListaEjecutor(ddlPPER.SelectedValue.ToString, ddlRegion.SelectedValue.ToString, ddlAmbito.SelectedValue.ToString)
        Me.fsSabana.Style("display") = "none"
    End Sub

    Protected Sub btnCerrar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCerrar.Click
        Response.Redirect("Inicio.aspx")
    End Sub

    Protected Sub ddlEjecutor_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlEjecutor.SelectedIndexChanged
        Me.fsSabana.Style("display") = "none"
    End Sub

    Protected Sub ddlPeriodo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPeriodo.SelectedIndexChanged
        Me.fsSabana.Style("display") = "none"
    End Sub

End Class
